package com.mouliu.multi;

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;

/**
 * @author mouliu
 * @create 2018-06-11-下午11:37
 */
public class Main {
    public static void main(String[] args)throws Exception{
        //创建ringBuffer
        RingBuffer<Order>ringBuffer =
                RingBuffer.create(ProducerType.MULTI, new EventFactory<Order>() {
                    @Override
                    public Order newInstance() {
                        return new Order();
                    }
                },1024*1024,new YieldingWaitStrategy());
        SequenceBarrier barrier = ringBuffer.newBarrier();
        Consumer[] consumers = new Consumer[3];
        for (int i=0;i<consumers.length;i++){
            consumers[i] = new Consumer("c"+i);
        }
        WorkerPool<Order>workerPool =
                new WorkerPool<Order>(ringBuffer,barrier,new IntEventExceptionHandler(),
                        consumers);
        ringBuffer.addGatingSequences(workerPool.getWorkerSequences());
        workerPool.start(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));

        final CountDownLatch latch =
                new CountDownLatch(1);
        for (int i=0;i<100;i++){
            final Producer p = new Producer(ringBuffer);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        latch.await();
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        Thread.sleep(2000);
        System.out.println("---------------开始生产-----------------");
        latch.countDown();
        Thread.sleep(5000);
        System.out.println("总数:" + consumers[0].getCount() );
    }

    static class IntEventExceptionHandler implements ExceptionHandler {
        public void handleEventException(Throwable ex, long sequence, Object event) {}
        public void handleOnStartException(Throwable ex) {}
        public void handleOnShutdownException(Throwable ex) {}
    }
}
